<?php
/**
 *+------------------
 * Gadmin 开源后台系统
 *+------------------
 * Copyright (c) 2006~2018 http://cojz8.cn All rights reserved.
 *+------------------
 * Author: guoguo(1838188896@qq.com)
 *+------------------
 */
 
namespace app\index\controller;

use app\common\controller\admin;
use think\Db;

class Index extends Admin
{
	/**
     * 系统首页
     */
    public function index()
    {
		$Node = obj2arr(model('Node')->getAllNode());
		$this->assign('Node',$Node);
		$username = session('user_name'); 
        $roleid   = session('roleid'); 
        if($username == config('rbac.admin_auth_key')){
            $sql = 'SELECT `id`,`title` FROM `g_node` WHERE ( `status` =1 AND `display`=1 AND `level`<>1 ) ORDER BY sort DESC';
        }else{
            $sql = "SELECT `g_node`.`id` as id,`g_node`.`title` as title FROM `g_node`,`g_access` WHERE `g_node`.id=`g_access`.node_id AND `g_access`.role_id=$roleid  AND  `g_node`.`status` =1 AND `g_node`.`display`=1 AND (`g_node`.`level` =0 OR `g_node`.`level` =2)  ORDER BY `g_node`.sort DESC";
        }
        $main_menu = Db::query($sql);
		foreach($main_menu as $k=>$v){
			$pid = $v['id']; 
			$NodeDB = model('Node');
			$datas = $this->left_child_menu($pid);

			$parent_info = $NodeDB->getNode(array('id'=>$pid),'title');
			$sub_menu_html = '';
			foreach($datas as $key => $_value) {
			$sub_menu_html .= '<dl>';
				$sub_array = $this->left_child_menu($_value['id']);
				$sub_menu_html .= "
				<dt><i class='Hui-iconfont'>&#xe616;</i> {$_value['title']}<i class='Hui-iconfont menu_dropdown-arrow'>&#xe6d5;</i></dt>
				<dd style='display: block;'><ul>";
				if(is_array($sub_array)){
					foreach ($sub_array as $value) {
						$href = empty($value['data']) ? 'javascript:void(0)' : url($value['data']);
						$sub_menu_html .= "<li><a data-href={$href} data-title={$value['title']} href='javascript:void(0)'>{$value['title']}</a></li>";
					}
				}
				$sub_menu_html .=  '</ul></dd>';
			$sub_menu_html .= '</dl>';
			}
			$main_menu[$k]['left'] = $sub_menu_html;
		}
        $this->assign('main_menu',$main_menu);
		return $this->fetch();
	}
	/**
     * 欢迎页面
     */
	public function welcome()
    {
		return $this->fetch();
    }
	 /**
     * 按父ID查找菜单子项
     * @param integer $parentid   父菜单ID
     * @param integer $with_self  是否包括他自己
     */
    private function left_child_menu($pid, $with_self = 0)
    {
        $pid = intval($pid);

        $username = session('user_name');    // 用户名
        $roleid   = session('roleid');      // 角色ID
        if($username == config('rbac.admin_auth_key')){     //如果是无视权限限制的用户，则获取所有主菜单
            $sql = "SELECT `id`,`data`,`title` FROM `g_node` WHERE ( `status` =1 AND `display`=2 AND `level` <>1 AND `pid`=$pid ) ORDER BY sort DESC";
        }else{
            $sql = "SELECT `g_node`.`id` as `id` , `g_node`.`data` as `data`, `g_node`.`title` as `title` FROM `g_node`,`g_access` WHERE `g_node`.id = `g_access`.node_id AND `g_access`.role_id = $roleid AND `g_node`.`pid` =$pid AND `g_node`.`status` =1 AND `g_node`.`display` =2 AND `g_node`.`level` <>1 ORDER BY `g_node`.sort DESC";
        }
        $result = Db::query($sql);

        if($with_self) {
            $NodeDB = D('Node');
            $result2[] = $NodeDB->getNode(array('id'=>$pid),`id`,`data`,`title`);
            $result = array_merge($result2,$result);
        }
        return $result;
    }
}
